import Http from "../fish/net/Http";
import Global from "../Global";
import ProtocolEnums from "../enums/ProtocolEnums";
import PlatformBase from "./PlatformBase";

export default class OPPOPlatform extends PlatformBase {
    private _sdk: any;
    private _sharetime: number;
    private _sharecallback: Function;
    private _videoCallback: Function;
    private _bannerAd: any;
    private _loginCallBcak: (data: any) => void;
    private _adLastUpdateTime: number;
    private _nativeAd: any;
    private _isNative: boolean;
    private _rewardAd: any;
    private _canShow: boolean = false;
    private _pause: boolean = false;
    constructor() {
        super();
        //初始化
        this._sdk = window['qg'];
        this.initRewardAd();
    }

    public login(callback: (data: any) => void): void {
        if (callback) {
            this._loginCallBcak = callback;
        } else {
            console.log("Login cb is null")
        }
        var self: any = this;
        this._sdk.login({
            success(res) {
                var param: any = {
                    platform: self.name(),
                    token: res.data.token,
                    resdata: res.data,
                    spId: 0,
                    shareId: 0,
                    zoneId: Global.isiOS ? 2 : 1,
                };
                Http.send(ProtocolEnums.LOGIN, { param: JSON.stringify(param) }, function (data: any) {
                    self._loginCallBcak(data);
                });
            },
            fail(res) {
                console.group('oppo登录失败信息');
                console.log(JSON.stringify(res));
            }
        });
    }

    public share(callback: (success: boolean) => void): void {
        callback(true);
    }

    public watchAd(callback: (success: boolean) => void, adID: string = ""): void {
        let self = this;
        console.log("激励广告", adID);
        self._videoCallback = callback;
        if (self._canShow) {
            self._pause = true;
            self.pauseAll();
            self._rewardAd.show();
        } else {
            self.resumeAll();
            self._videoCallback(false);
            self._rewardAd.load();
        }

    }
    private initRewardAd() {
        let self = this;
        self._rewardAd = this._sdk.createRewardedVideoAd({
            posId: '1111111'
        });
        var onclose = function (res) {
            if (self._videoCallback) {
                self._videoCallback(res && res.isEnded || res === undefined);
                self._rewardAd.load();
            }
            if (self._pause) {
                self.resumeAll();
                self._pause = false;
            }
        }.bind(self._rewardAd);
        var onshow = function () {
            self._canShow = true;
            console.log("加载成功");
        }.bind(self._rewardAd);
        var onerror = function (res) {
            if (self._pause) {
                self.resumeAll();
                self._pause = false;
            }
            self._canShow = false;
            self._videoCallback(res && res.isEnded || res === undefined);
            console.log("加载失败");
            console.log(JSON.stringify(res));
        }.bind(self._rewardAd);
        self._rewardAd.onError(onerror);
        self._rewardAd.onClose(onclose);
        self._rewardAd.onLoad(onshow);
        self._rewardAd.load();
        console.log("激励广告load");
    }
    public showBannerad() {

        if (this._bannerAd) {
            this._bannerAd.destroy();
            this._bannerAd = null;
        }
        var bannerAd = this._sdk.createBannerAd({
            posId: "11111"
        })
        bannerAd.onError(function (err) {
            console.error("showBannerad!" + JSON.stringify(err))
        }.bind(this));
        bannerAd.show();
        this._bannerAd = bannerAd;
    }

    public hideBanner() {
        if (!this._bannerAd) return;
        this._bannerAd.destroy();
        this._bannerAd = null;
    }

    public adShare(callback: (success: boolean) => void): void {
        callback(true);
    }
    public name(): string {
        return "OPPOGame";
    }

    public initAdService() {
        let self = this;
        this._sdk.initAdService({
            appId: "11111",
            isDebug: true,
            success: function (res) {
                console.log("oppo _rewardAd init success");
                self.initRewardAd();
            },
            fail: function (res) {
                console.log("fail:" + res.code + res.msg);
            },
            complete: function (res) {
                console.log("complete");
            }
        })

    }

    public createNativeAd() {
        let randNum = Math.floor(Math.random() * 100);
        if (randNum > 40) {
            this.showBannerad();
            this._isNative = false;
            return;
        } else {
            this._isNative = true;
        }
        if (this._nativeAd) {
            this._nativeAd.destroy()
            this._nativeAd = null;
        }
        this._nativeAd = this._sdk.createNativeAd({
            posId: '1111111'
        });
        this._nativeAd.load()
        this._nativeAd.onLoad((res) => {
            this._nativeAd.reportAdShow({
                adId: res.adList[0].adId
            });
            //为插屏广告创建一个预制体展示
            UIMgr.dispatch(UIEnums.QGNATIVEAD, MsgEnums.NATIVE_SHOW, {
                url: res.adList[0].imgUrlList[0],
                title: res.adList[0].title,
                desc: res.adList[0].desc,
                cb: () => {
                    this._nativeAd.reportAdClick({
                        adId: res.adList[0].adId,
                    })
                }
            });
            // console.log('原生广告加载', JSON.stringify(res));
            this._nativeAd.offLoad();
            this._nativeAd.offError();

        })

        this._nativeAd.onError((err) => {
            console.log(JSON.stringify(err));
            console.log('原生广告加载失败');
            this._nativeAd.offLoad();
            this._nativeAd.offError();
            this._nativeAd.destroy();
            this._nativeAd = null;
        })
    }

    public hideNativeAd() {
        if (this._isNative) {

        } else {
            this.hideBanner();
        }

    }

    public pauseAll() { }

    public resumeAll() { }
}